<?php
Namespace Api\Controller;
use Think\Controller;
use Api\Model;
class ApiBaseController extends Controller {
    function getH5BaseUrl(){
        $strH5BaseUrl="http://appapi.anniekids.net/release/index.php/home/";
        return $strH5BaseUrl;
    }

    //获取城市
    public function getCities(){
        $mdlHr =new Model\HrModel();
        ShowJsonReturnAndDie($mdlHr->getCityList());
    }
    public function getDefaultUserName()
    {
        $strUsername = trim(I("username"));
        return $strUsername;
    }

    //获取学校
    public function getSchools()
    {
        $mdlHr =new \Api\Model\HrModel();
        $strCity =I("city");
        ShowJsonReturnAndDie($mdlHr->getSchools($strCity,"schoolId","school"));
    }
    //检查用户名
    public function checkUsers(){
        $strSchoolID =  I("schoolId");
        $strName = I("name");
        $mdlParent =new Model\AppParentsModel();
        ShowJsonReturnAndDie($mdlParent->checkUsers($strSchoolID,$strName));
    }

    /**
     * 将8位时间转换成年-月-日的格式
     * @param mixed $strInputStr 
     * @return string
     */
    function DatetimeToDbFormat($strInputStr){
        $strDate =  substr($strInputStr,0,4)."-".substr($strInputStr,4,2)."-".substr($strInputStr,6,2);
        if(!checkDateIsValid($strDate)){
            ShowJsonReturnAndDie(null,true,"请输入有效的日期格式");
        }
        return $strDate;
    }

    //检查父级
    function checkAndGetParentID($strToken)
    {
        if(($strToken==null)||(trim($strToken)==""))
        {
           ShowJsonLoginAndDie();
        }
        $mdlParent =new \Api\Model\AppParentsModel();
        $iParentID = $mdlParent->getIdByToken($strToken);
        if($iParentID<1){ShowJsonLoginAndDie();}
        return $iParentID;
    }
    
    /**
     * 保存上传文件并返回文件路径
     * @param mixed $strSaveDirName 
     * @return mixed
     */
    function saveUploadFile($strSaveDirName)
    {
        //引入UploadFile类
        import('ORG.Net.UploadFile');
        //实例化UploadFile类
        $upload  = new \UploadFile();
        //设置文件大小
        $upload -> maxSize = 3292200;
        //设置文件保存规则唯一
        $upload->saveRule = 'uniqid';
        
        $upload->subType = 'date';
        //设置上传文件的格式
        $upload -> allowExts = array('jpg','png','jpeg','pcm','wav','mp3');
        $upload->autoSub=true;
        //保存路径
        $upload->savePath ='./Public/Uploads/'.$strSaveDirName.'/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = false;
        //上传失败返回错误信息
        if(!$upload->upload()){
            ShowJsonReturnAndDie(null,true,$upload->getErrorMsg());
            //ShowJsonReturnAndDie(null,true,"上传失败请稍后再试!");
            //$this->error($upload->getErrorMsg());
        }   //获取上传文件的信息
        $arrInfoes= $upload->getUploadFileInfo();
        $info=$arrInfoes[0];
        $strFilePath =$info["savename"];
        return $strSaveDirName."/".$strFilePath;
    }
  }
?>